//#ifndef VIMAGEVIEWER_H
//#define VIMAGEVIEWER_H

static BOOLEAN isRomanManager = false;


class TDownloadFiles: public QObject{
	Q_OBJECT	
		public slots:	
			void finishedDownload();			
			void errorDownload(QNetworkReply::NetworkError error);
			void replyFinished(QNetworkReply* );			
			void setValueDownload(qint64, qint64);	
			void replyDestroyed();
			void managerDestroyed();
protected:

	QNetworkReply * currentReply;	
	QString curFilter;		
	void doNetworkRequest();
	void startDownloadImageList();	
	void saveToFiles();	
	QThreadPool qThreadPool;		
	QNetworkAccessManager * networkManager;	
	BOOLEAN noMessage;	
	int numberFileSave;
	int destroyedReplies;
	QList<QNetworkReply *>networkReplies;
	unsigned int unSavedFilesSize;	
	QUrl Url;
	QStringList UIDS;
public:	
	BOOLEAN isTerminate;	
	BOOLEAN isNeedTerminate;
	~TDownloadFiles();
	void startDownloadImages();	
	TDownloadFiles(const QStringList&, QUrl Url, QString appDir, BOOLEAN noMessage, QString curFilter, int defNumber);	
	BOOLEAN isSavingFilesToDisk;
	//BOOLEAN wasLocked;
	BOOLEAN getIsSavingFilesToDisk();
	int getTotal();
	int getDone();
	int getNumberFile();
	bool getIsTerminate();
	void setIsTerminate(bool isTerminate);
	qint64 done;
	qint64 total;
	int numberFile;	
	int defNumber;
	QString appDir;
private:
	QMutex mutex;
};


class TDownloadFilesThread: public QThread{
	Q_OBJECT	
public:
	QString appDir;
	TDownloadFiles * downloadFiles;	
	QString curFilter;
	QUrl Url;
	BOOLEAN noMessage;
	TDownloadFilesThread(QStringList _UIDS, QUrl _Url, QString _appDir, BOOLEAN _noMessage, QString _curFilter, int _defNumber);
	~TDownloadFilesThread();
	QStringList UIDS;
	bool isCreated();
protected:
	int defNumber;
	void run();	
	QMutex mutex;
};


class TDownloadFilesMultiThread: public QObject{
	Q_OBJECT	
public slots:
	void replyFinishedINI(QNetworkReply* qReply);
	void setValueDownloadINI(qint64 done, qint64 total);
	void errorDownloadINI( QNetworkReply::NetworkError error );
public:	
signals:
	void startedAllThreads();
public:
	TDownloadFilesMultiThread();
	void init(QUrl url, int NumberThreads, BOOLEAN noMessage, QString curFilter);
	~TDownloadFilesMultiThread();
	void startDownloadImageList(QUrl url);	
	BOOLEAN isTerminateAll();
	int getNumberFile();
	int getDone();
	int getTotal();
	BOOLEAN isSavingFilesToDisk();		
	QTime startTime;	
	QStringList UIDS;
	BOOLEAN isTerminate;
	BOOLEAN isNeedTerminate;
private:
	QNetworkAccessManager networkManager;
	int NumberThreads;
	QUrl imageListURL;
	BOOLEAN noMessage;
	QString curFilter;
	QList<TDownloadFilesThread *> threads;		
};


class TDownloadFilesDialog : public QDialog{
	Q_OBJECT				
public:
signals:	
	void signalDownload(int time, int numberFile, int size, int done, bool isSavingFilesToDisk);	
	//void init(QString curFilter = "");
public:
	QProgressBar * progress;
	QProgressBar * progressAll;	
	TDownloadFilesDialog(QUrl url, QString curFilter = "", BOOLEAN noMessage = true);
	~TDownloadFilesDialog();
	void timerEvent(QTimerEvent * event);		
	TDownloadFilesMultiThread downloadFilesMultiThread;
private:
	int timer;
	QPushButton * ok;
	QLabel label;
	QLabel label2;
	BOOLEAN noMessage;
public slots:
	void startT();
};

class SaveFileWorker : public QObject, public QRunnable {
	Q_OBJECT
	QString appDir;
	int fileId;
	QByteArray byteArray;	
	QString filter;
public:
	SaveFileWorker(const QByteArray& byteArray, int fileId, QString filter, const QString& appDir){
		this->byteArray = byteArray;
		this->fileId = fileId;
		this->filter = filter;
		this->appDir = appDir;
	}	

protected:
	void run();
};
								